package edu.auctionmart

import java.util.Comparator;

class AuctionService {

	def Bid findHighestBid(Auction auction) {
		//Find the 2nd highest bid to determine the new current bid for the highest bidder
		List<Bid> bidList = new ArrayList<Bid>(auction.bids)
		Collections.sort(bidList, new BidComparator())
		Bid secondHighest = bidList.get(bidList.size() - 2)
		Bid highest = bidList.get(bidList.size() - 1)
		return new Bid(auction: auction, userAccount: highest.getUserAccount(),
			amount: secondHighest.getAmount(), time: highest.getTime())
	}
	
	class BidComparator implements Comparator {
		
		@Override
		public int compare(Object o1, Object o2) {
			if (o1 instanceof Bid && o2 instanceof Bid) {
				Bid bidOne = (Bid) o1;
				Bid bidTwo = (Bid) o2;
				return bidOne.getAmount().compareTo(bidTwo.getAmount())
			}
			else 
				return 0;
		}
	}
}
